import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class PrinterService extends BaseService<any> {
  private readonly logger = new Logger(PrinterService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: PrinterService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,print_width,status,create_time';         $order = 'create_time...
const field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,print_width,status,create_time';
        const order = 'create_time desc';

        const search_model = this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "printer_name" ), where).field(field).order(order).append([ 'brand_name' )];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * 获取小票打印机列表
     * @param where
     * @param string field
     * @return * @throws \think\db\exception\DbBusinessException
     */
    async getList(where = [), field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,print_width,status,create_time')
    {
        return ( this.corePrinterService ).getList(where, field);
    }

    /**
     * 获取小票打印机信息
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,value,print_width,status';

        const info = this.model.field(field).where([ [ 'printer_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印机
     * @param data
     * @return mixed
     */
    async add(data)
    {
        try {
            Db.startTrans(];

            data[ 'site_id' ] = this.site_id;
            const res = this.model.create(data);

            // 绑定易联云设备授权
            if (data[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).addPrinterYly(this.site_id, data);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            Db.commit(];
            return res.printer_id;
  }

  /**
   * getList
   * 对应 PHP: PrinterService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[], field: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: return ( new CorePrinterService() )->getList($where, $field);     }      /**      * 获取小票打印机信息      * @param int $id      * @return array      */     p...
return ( this.corePrinterService ).getList(where, field);
    }

    /**
     * 获取小票打印机信息
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,value,print_width,status';

        const info = this.model.field(field).where([ [ 'printer_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印机
     * @param data
     * @return mixed
     */
    async add(data)
    {
        try {
            Db.startTrans(];

            data[ 'site_id' ] = this.site_id;
            const res = this.model.create(data);

            // 绑定易联云设备授权
            if (data[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).addPrinterYly(this.site_id, data);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            Db.commit(];
            return res.printer_id;
  }

  /**
   * getInfo
   * 对应 PHP: PrinterService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,value,print_width,status';          $info = $this->model->fiel...
const field = 'printer_id,site_id,brand,printer_name,printer_code,printer_key,open_id,apikey,value,print_width,status';

        const info = this.model.field(field).where([ [ 'printer_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印机
     * @param data
     * @return mixed
     */
    async add(data)
    {
        try {
            Db.startTrans(];

            data[ 'site_id' ] = this.site_id;
            const res = this.model.create(data);

            // 绑定易联云设备授权
            if (data[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).addPrinterYly(this.site_id, data);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            Db.commit(];
            return res.printer_id;
  }

  /**
   * add
   * 对应 PHP: PrinterService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: try {             Db::startTrans();              $data[ 'site_id' ] = $this->site_id;             $res = $this->model->create($data);              // ...
try {
            Db.startTrans(];

            data[ 'site_id' ] = this.site_id;
            const res = this.model.create(data);

            // 绑定易联云设备授权
            if (data[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).addPrinterYly(this.site_id, data);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            Db.commit(];
            return res.printer_id;
  }

  /**
   * edit
   * 对应 PHP: PrinterService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: try {             Db::startTrans();              // 绑定易联云设备授权             if ($data[ 'brand' ] == PrinterDict::YI_LIAN_YUN) {                 $result ...
try {
            Db.startTrans(];

            // 绑定易联云设备授权
            if (data[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).addPrinterYly(this.site_id, data);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            this.model.where([ [ 'printer_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data];
            Db.commit();
            return true;
  }

  /**
   * modifyStatus
   * 对应 PHP: PrinterService_admin::modifyStatus()
   * 逻辑类型: undefined - undefined
   */
  async modifyStatus(data: any) {
    // 基于PHP真实逻辑: modifyStatus
    // PHP原文: return $this->model->where([             [ 'printer_id', '=', $data[ 'printer_id' ] ],             [ 'site_id', '=', $this->site_id ]         ])->upda...
return this.model.where([
            [ 'printer_id', '=', data[ 'printer_id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 删除小票打印机
     * @param int printer_id
     * @return bool
     */
    async del(int printer_id)
    {
        try {
            Db.startTrans(];
            const field = 'printer_id,brand,printer_code,open_id,apikey';
            const printer_info = this.model.field(field).where([ [ 'site_id', '=', this.site_id ], [ 'printer_id', "=", printer_id ) )).findOrEmpty().toArray(];

            // 删除易联云打印机授权
            if (printer_info[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).deletePrinterYly(this.site_id, printer_info);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            const model = this.model.where([ [ 'printer_id', '=', printer_id ], [ 'site_id', '=', this.site_id ) )).find(];
            const res = model.delete(];
            Db.commit();
            return res;
  }

  /**
   * del
   * 对应 PHP: PrinterService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(printer_id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: try {             Db::startTrans();             $field = 'printer_id,brand,printer_code,open_id,apikey';             $printer_info = $this->model->fie...
try {
            Db.startTrans(];
            const field = 'printer_id,brand,printer_code,open_id,apikey';
            const printer_info = this.model.field(field).where([ [ 'site_id', '=', this.site_id ], [ 'printer_id', "=", printer_id ) )).findOrEmpty().toArray(];

            // 删除易联云打印机授权
            if (printer_info[ 'brand' ) == PrinterDict.YI_LIAN_YUN) {
                const result = ( this.corePrinterService ).deletePrinterYly(this.site_id, printer_info);
                if (result[ 'code' ) != 0) {
                    Db.rollback(];
                    throw new BusinessException(result[ 'message' );
  }
            const model = this.model.where([ [ 'printer_id', '=', printer_id ], [ 'site_id', '=', this.site_id ) )).find(];
            const res = model.delete(];
            Db.commit();
            return res;
  }

  /**
   * getType
   * 对应 PHP: PrinterService_admin::getType()
   * 逻辑类型: undefined - undefined
   */
  async getType() {
    // 基于PHP真实逻辑: getType
    // PHP原文: return ( new CorePrinterService() )->getType();     }      /**      * 获取打印机设备品牌      * @param $brand      * @return array|mixed|string      */     pub...
return ( this.corePrinterService ).getType(];
    }

    /**
     * 获取打印机设备品牌
     * @param brand
     * @return array|mixed|string
     */
    async getBrand(brand)
    {
        return ( this.corePrinterService ).getBrand(brand);
    }

    /**************************************************** 打印机管理（第三方） *********************************************************/

    /******************** 易联云 start ************************/

    /**
     * 设置易联云小票打印token
     * @param data
     * @return \app\model\sys\SysConfig|bool|\think\Model
     */
    async setYlyTokenConfig(data)
    {
        return ( this.corePrinterService ).setYlyTokenConfig(this.site_id, data);
    }

    /**
     * 获取易联云配置
     * @return */
    async getYlyTokenConfig()
    {
        return ( this.corePrinterService ).getYlyTokenConfig(this.site_id);
    }

    /**
     * 重新获取易联云token
     * @param printer_id
     * @return mixed
     */
    async refreshToken(printer_id)
    {
        return ( this.corePrinterService ).refreshToken(this.site_id, printer_id);
    }

    /**
     * 测试打印
     * @param printer_id
     * @return */
    async testPrint(printer_id)
    {
        return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * getBrand
   * 对应 PHP: PrinterService_admin::getBrand()
   * 逻辑类型: undefined - undefined
   */
  async getBrand(brand: any) {
    // 基于PHP真实逻辑: getBrand
    // PHP原文: return ( new CorePrinterService() )->getBrand($brand);     }      /**************************************************** 打印机管理（第三方） *******************...
return ( this.corePrinterService ).getBrand(brand);
    }

    /**************************************************** 打印机管理（第三方） *********************************************************/

    /******************** 易联云 start ************************/

    /**
     * 设置易联云小票打印token
     * @param data
     * @return \app\model\sys\SysConfig|bool|\think\Model
     */
    async setYlyTokenConfig(data)
    {
        return ( this.corePrinterService ).setYlyTokenConfig(this.site_id, data);
    }

    /**
     * 获取易联云配置
     * @return */
    async getYlyTokenConfig()
    {
        return ( this.corePrinterService ).getYlyTokenConfig(this.site_id);
    }

    /**
     * 重新获取易联云token
     * @param printer_id
     * @return mixed
     */
    async refreshToken(printer_id)
    {
        return ( this.corePrinterService ).refreshToken(this.site_id, printer_id);
    }

    /**
     * 测试打印
     * @param printer_id
     * @return */
    async testPrint(printer_id)
    {
        return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * setYlyTokenConfig
   * 对应 PHP: PrinterService_admin::setYlyTokenConfig()
   * 逻辑类型: undefined - undefined
   */
  async setYlyTokenConfig(data: any) {
    // 基于PHP真实逻辑: setYlyTokenConfig
    // PHP原文: return ( new CorePrinterService() )->setYlyTokenConfig($this->site_id, $data);     }      /**      * 获取易联云配置      * @return array      */     public f...
return ( this.corePrinterService ).setYlyTokenConfig(this.site_id, data);
    }

    /**
     * 获取易联云配置
     * @return */
    async getYlyTokenConfig()
    {
        return ( this.corePrinterService ).getYlyTokenConfig(this.site_id);
    }

    /**
     * 重新获取易联云token
     * @param printer_id
     * @return mixed
     */
    async refreshToken(printer_id)
    {
        return ( this.corePrinterService ).refreshToken(this.site_id, printer_id);
    }

    /**
     * 测试打印
     * @param printer_id
     * @return */
    async testPrint(printer_id)
    {
        return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * getYlyTokenConfig
   * 对应 PHP: PrinterService_admin::getYlyTokenConfig()
   * 逻辑类型: undefined - undefined
   */
  async getYlyTokenConfig() {
    // 基于PHP真实逻辑: getYlyTokenConfig
    // PHP原文: return ( new CorePrinterService() )->getYlyTokenConfig($this->site_id);     }      /**      * 重新获取易联云token      * @param $printer_id      * @return mi...
return ( this.corePrinterService ).getYlyTokenConfig(this.site_id);
    }

    /**
     * 重新获取易联云token
     * @param printer_id
     * @return mixed
     */
    async refreshToken(printer_id)
    {
        return ( this.corePrinterService ).refreshToken(this.site_id, printer_id);
    }

    /**
     * 测试打印
     * @param printer_id
     * @return */
    async testPrint(printer_id)
    {
        return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * refreshToken
   * 对应 PHP: PrinterService_admin::refreshToken()
   * 逻辑类型: undefined - undefined
   */
  async refreshToken(printer_id: number) {
    // 基于PHP真实逻辑: refreshToken
    // PHP原文: return ( new CorePrinterService() )->refreshToken($this->site_id, $printer_id);     }      /**      * 测试打印      * @param $printer_id      * @return ar...
return ( this.corePrinterService ).refreshToken(this.site_id, printer_id);
    }

    /**
     * 测试打印
     * @param printer_id
     * @return */
    async testPrint(printer_id)
    {
        return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * testPrint
   * 对应 PHP: PrinterService_admin::testPrint()
   * 逻辑类型: undefined - undefined
   */
  async testPrint(printer_id: number) {
    // 基于PHP真实逻辑: testPrint
    // PHP原文: return ( new CorePrinterService() )->testPrint($this->site_id, $printer_id);     }      /**      * 打印小票内容      * @param $params      * @return array  ...
return ( this.corePrinterService ).testPrint(this.site_id, printer_id);
    }

    /**
     * 打印小票内容
     * @param params
     * @return */
    async printTicket(params)
    {
        params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }

  /**
   * printTicket
   * 对应 PHP: PrinterService_admin::printTicket()
   * 逻辑类型: undefined - undefined
   */
  async printTicket(params: any) {
    // 基于PHP真实逻辑: printTicket
    // PHP原文: $params[ 'site_id' ] = $this->site_id;         return ( new CorePrinterService() )->printTicket($params);     }      /******************** 易联云 end ***...
params[ 'site_id' ] = this.site_id;
        return ( this.corePrinterService ).printTicket(params);
    }

    /******************** 易联云 end ************************/

}
  }
}
